from time import sleep

import pytest
import requests

from api import log
from api.api_register_login import ApiRegisterLogin
from tool import common_assert, read_json


class TestRegisterLogin:
    # 初始化
    def setup_method(self):
        # 获取session对象
        self.session = requests.session()
        log.info("正在获取session对象：{}".format(self.session))
        # 获取ApiRegisterLogin示例对象
        self.api = ApiRegisterLogin(self.session)
        log.info("正在获取ApiRegisterLogin对象：{}".format(self.api))

    # 结束
    def teardown(self):
        # 关闭session
        self.session.close()
        log.info("关闭session对象：{}".format(self.session))

    # 1、图片验证码接口 测试
    @pytest.mark.parametrize("random,expect_code", read_json("register_login.json", "img_code"))
    def test01_img_code(self, random, expect_code):
        try:
            result = self.api.api_img_code(random)
            log.info("图片验证码接口测试结果为：{}".format(result.status_code))
            common_assert(result, status_code=expect_code)
            log.info("图片验证码接口断言通过！")
        except Exception as e:
            # 捕获异常的目的就是记录错误日志
            log.error(e)
            # 抛异常
            raise

    # 2、短信验证码接口 测试
    @pytest.mark.parametrize("phone,imgVerifyCode,expect_json", read_json("register_login.json", "phone_code"))
    def test02_phone_code(self, phone, imgVerifyCode, expect_json):
        try:
            # 1、请求图片验证码
            self.api.api_img_code(123123)
            # 2、调用手机短信接口
            result = self.api.api_phone_code(phone, imgVerifyCode)
            log.info("短信验证码接口测试结果为：{}".format(result.text))
            common_assert(result, expect_json=expect_json)
            log.info("短信验证码接口断言通过！")
        except Exception as e:
            # 捕获异常的目的就是记录错误日志
            log.error(e)
            # 抛异常
            raise

    # 3、注册接口 测试
    @pytest.mark.parametrize("phone,password,imgVerifyCode,phone_code,expect_json",
                             read_json("register_login.json", "register"))
    def test03_register(self, phone, password, imgVerifyCode, phone_code, expect_json):
        try:
            # 1、调用图片验证码
            self.api.api_img_code(123123)
            # 2、调用手机短信验证码
            self.api.api_phone_code(phone, imgVerifyCode)
            # 3、调用注册接口
            result = self.api.api_register(phone, password, verifycode=imgVerifyCode, phone_code=phone_code)
            log.info("注册接口测试结果为：{}".format(result.text))
            common_assert(result, expect_json=expect_json)
            log.info("注册接口断言通过！")
        except Exception as e:
            # 捕获异常的目的就是记录错误日志
            log.error(e)
            # 抛异常
            raise

    # 4、登录接口 测试
    @pytest.mark.parametrize("keywords,password,expect_json", read_json("register_login.json", "login"))
    def test04_login(self, keywords, password, expect_json):
        try:
            i = 1
            result = None
            if password == "error 123":
                while i <= 3:
                    result = self.api.api_login(keywords, password)
                    log.info("登录接口测试结果为：{}".format(result.text))
                    i += 1
                assert "锁定" in result.text
                log.info("登录->锁定 断言通过！")

                sleep(60)
                # 登录成功->判断解锁
                result = self.api.api_login("13600001111", "test123")
                log.info("登录测试结果为：{}".format(result.text))
                common_assert(result, expect_json=expect_json)
                log.info("登录断言通过！")

            else:
                result = self.api.api_login(keywords, password)
                common_assert(result, expect_json=expect_json)
        except Exception as e:
            # 捕获异常的目的就是记录错误日志
            log.error(e)
            # 抛异常
            raise

    # 5、查询登录状态接口 测试
    @pytest.mark.parametrize("status,expect_json", read_json("register_login.json", "login_status"))
    def test05_login_status(self, status, expect_json):
        try:
            if status == "已登录":
                # 调用登录
                self.api.api_login("13600001111", "test123")
            # 调用查询登录状态接口
            result = self.api.api_login_status()
            log.info("查询登录状态测试结果为：{}".format(result.text))
            common_assert(result, expect_json=expect_json)
            log.info("查询登录状态 断言通过！")
        except Exception as e:
            # 捕获异常的目的就是记录错误日志
            log.error(e)
            # 抛异常
            raise
